
package laya.semi.process;

import java.util.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.*;

import org.apache.log4j.*;

import laya.*;
import laya.util.*;
import laya.xml.sax.*;

public class ProcessConfig extends DefaultHandler
{
	private static Logger log = Logger.getLogger(ProcessConfig.class);

	public static final String TAG_ROOT = LayaGlobals.LAYA_NAMESPACE + ":" + "Process";
	public static final String TAG_HEAD = LayaGlobals.LAYA_NAMESPACE + ":" + "Head";
	public static final String TAG_NAME = LayaGlobals.LAYA_NAMESPACE + ":" + "Name";
	public static final String TAG_VERSION = LayaGlobals.LAYA_NAMESPACE + ":" + "Version";
	public static final String TAG_DESCRIPTION = LayaGlobals.LAYA_NAMESPACE + ":" + "Description";
	public static final String TAG_PERSISTENCEHANDLER = LayaGlobals.LAYA_NAMESPACE + ":" + "PersistenceHandler";
	public static final String TAG_ACTIVITYHANDLER = LayaGlobals.LAYA_NAMESPACE + ":" + "ActivityHandler";
	public static final String TAG_FIELDMAPPINGS = LayaGlobals.LAYA_NAMESPACE + ":" + "FieldMappings";
	public static final String TAG_FIELD = LayaGlobals.LAYA_NAMESPACE + ":" + "Field";
	public static final String TAG_STATES = LayaGlobals.LAYA_NAMESPACE + ":" + "States";
	public static final String TAG_STATE = LayaGlobals.LAYA_NAMESPACE + ":" + "State";
	public static final String TAG_STATETYPE = LayaGlobals.LAYA_NAMESPACE + ":" + "StateType";
	public static final String TAG_STATEDESCRIPTION = LayaGlobals.LAYA_NAMESPACE + ":" + "StateDescription";
	public static final String TAG_STATEINHIBITS = LayaGlobals.LAYA_NAMESPACE + ":" + "Inhibits";
	public static final String TAG_STATECONDITION = LayaGlobals.LAYA_NAMESPACE + ":" + "Condition";
	public static final String TAG_STATECONDITIONEXPRESSION = LayaGlobals.LAYA_NAMESPACE + ":" + "ConditionExpression";
	public static final String TAG_STATECONDITIONCLASS = LayaGlobals.LAYA_NAMESPACE + ":" + "ConditionClass";
	public static final String TAG_STATECONDITIONDESCRIPTION = LayaGlobals.LAYA_NAMESPACE + ":" + "ConditionDescription";
	public static final String TAG_STATEACTIVITIES = LayaGlobals.LAYA_NAMESPACE + ":" + "Activities";
	public static final String TAG_STATEACTIVITY = LayaGlobals.LAYA_NAMESPACE + ":" + "Activity";
	public static final String TAG_TRANSITIONS = LayaGlobals.LAYA_NAMESPACE + ":" + "Transitions";
	public static final String TAG_TRANSITION = LayaGlobals.LAYA_NAMESPACE + ":" + "Transition";
	public static final String TAG_TRANSITIONFROMSTATE = LayaGlobals.LAYA_NAMESPACE + ":" + "FromState";
	public static final String TAG_TRANSITIONTOSTATE = LayaGlobals.LAYA_NAMESPACE + ":" + "ToState";
	public static final String TAG_TRANSITIONINHIBITS = LayaGlobals.LAYA_NAMESPACE + ":" + "Inhibits";
	public static final String TAG_TRANSITIONCONDITION = LayaGlobals.LAYA_NAMESPACE + ":" + "Condition";
	public static final String TAG_TRANSITIONCONDITIONEXPRESSION = LayaGlobals.LAYA_NAMESPACE + ":" + "ConditionExpression";
	public static final String TAG_TRANSITIONCONDITIONCLASS = LayaGlobals.LAYA_NAMESPACE + ":" + "ConditionClass";
	public static final String TAG_TRANSITIONCONDITIONDESCRIPTION = LayaGlobals.LAYA_NAMESPACE + ":" + "ConditionDescription";
	
	public static final String ATTRIBUTE_ID = "id";
	public static final String ATTRIBUTE_TYPE = "type";
	public static final String ATTRIBUTE_PROTOTYPE = "prototype";
	public static final String ATTRIBUTE_DEFAULT = "default";
	public static final String ATTRIBUTE_FORMAT = "format";
	public static final String ATTRIBUTE_CONVERTOR = "convertor";
	public static final String ATTRIBUTE_ISKEY = "isKey";
	public static final String ATTRIBUTE_PARAMETER = "parameter";
	
	public static final String STATE_START_VALUE = "start";
	public static final String STATE_END_VALUE = "end";
	public static final String STATE_ACTIVITY_VALUE = "activity";
	// public static final String STATE_INPUT_VALUE = "input";
	public static final String STATE_FORK_VALUE = "fork";
	public static final String STATE_JOIN_VALUE = "join";
	
	public static final int STATE_READY = 0;
	public static final int STATE_START = 1;
	public static final int STATE_END_ROOT = 9998;
	public static final int STATE_END = 9999;
	
	// head body
	public static final int STATE_START_HEAD = 100;
	public static final int STATE_START_NAME = 110;
	public static final int STATE_GET_NAME = 111;
	public static final int STATE_START_VERSION = 120;
	public static final int STATE_GET_VERSION = 121;
	public static final int STATE_START_DESCRIPTION = 130;
	public static final int STATE_GET_DESCRIPTION = 131;
	public static final int STATE_START_PERSISTENCEHANDLER = 140;
	public static final int STATE_GET_PERSISTENCEHANDLER = 141;
	public static final int STATE_START_ACTIVITYHANDLER = 150;
	public static final int STATE_GET_ACTIVITYHANDLER = 151;
	public static final int STATE_END_HEAD = 199;
	
	// field mappings body
	public static final int STATE_START_FIELDMAPPINGS = 200;
	public static final int STATE_START_FIELD = 210;
	public static final int STATE_GET_FIELD = 211;
	
	// states body
	public static final int STATE_START_STATES = 1000;
	public static final int STATE_START_STATE = 1100;
	public static final int STATE_START_STATETYPE = 1110;
	public static final int STATE_GET_STATETYPE = 1111;
	public static final int STATE_START_STATEDESCRIPTION = 1120;
	public static final int STATE_GET_STATEDESCRIPTION = 1121;
	
	public static final int STATE_START_STATEINHIBITS = 1200;
	public static final int STATE_START_STATECONDITION = 1210;
	public static final int STATE_GET_STATECONDITION = 1211;
	public static final int STATE_GET_STATECONDITIONEXPR = 1212;
	public static final int STATE_GET_STATECONDITIONCLASS = 1213;
	public static final int STATE_START_STATECONDITIONDESCRIPTION = 1215;
	public static final int STATE_GET_STATECONDITIONDESCRIPTION = 1216;
	public static final int STATE_END_STATECONDITION = 1219;
	
	public static final int STATE_START_STATEACTIVITIES = 1300;
	public static final int STATE_START_STATEACTIVITY = 1310;
	public static final int STATE_GET_STATEACTIVITY = 1311;
	public static final int STATE_END_STATE = 1998;
	
	// transitions body
	public static final int STATE_START_TRANSITIONS = 2000;
	public static final int STATE_START_TRANSITION = 2100;
	public static final int STATE_START_TRANSITIONFROMSTATE = 2110;
	public static final int STATE_GET_TRANSITIONFROMSTATE = 2111;
	public static final int STATE_START_TRANSITIONTOSTATE = 2120;
	public static final int STATE_GET_TRANSITIONTOSTATE = 2121;
	
	public static final int STATE_START_TRANSITIONINHIBITS = 2200;
	public static final int STATE_START_TRANSITIONCONDITION = 2210;
	public static final int STATE_GET_TRANSITIONCONDITION = 2211;
	public static final int STATE_GET_TRANSITIONCONDITIONEXPR = 2212;
	public static final int STATE_GET_TRANSITIONCONDITIONCLASS = 2213;
	public static final int STATE_START_TRANSITIONCONDITIONDESCRIPTION = 2215;
	public static final int STATE_GET_TRANSITIONCONDITIONDESCRIPTION = 2216;
	public static final int STATE_END_TRANSITIONCONDITION = 2219;
	
	public static final int STATE_END_TRANSITION = 2998;
	
	private int m_nStep = STATE_READY;
	private Map m_oFieldTab = null;
	private Map m_oStateTab = null;
	private Vector m_oStateIdList = null;
	private StringBuffer m_oContentBuffer = null;
	private FieldMapping m_oCurrFieldMapping = null;
	private State m_oCurrState = null;
	private Transition m_oCurrTransition = null;
	private Condition m_oCurrCond = null;
	private Activity m_oCurrActi = null;
	
	private String m_oProcessName = null;
	private String m_oProcessVersion = null;
	private String m_oProcessDescription = null;
	private String m_oPhClassName = null; // persistence handler class name
	private String m_oAhClassName = null; // activity handler class name

	public ProcessConfig(String oProcessName, String oProcessVersion)
	{
		m_oProcessName = oProcessName;
		m_oProcessVersion = oProcessVersion;
	}
	
	public String getProcessName()
	{
		return m_oProcessName;
	}
	
	public String getProcessVersion()
	{
		return m_oProcessVersion;
	}
	
	public String getPrcessDescription()
	{
		return m_oProcessDescription;
	}
	
	public String getPersistenceHandlerName()
	{
		return m_oPhClassName;
	}
	
	public String getActivityHandlerName()
	{
		return m_oAhClassName;
	}
	
	public List getStateIdList()
	{
		return m_oStateIdList;
	}
	
	public State getState(String oStateId)
	{
		return (State)m_oStateTab.get(oStateId);
	}
	
	public FieldMapping getFieldMapping(String oFieldId)
	{
		return (FieldMapping)m_oFieldTab.get(oFieldId);
	}
	
	public Collection getFieldMappings()
	{
		return m_oFieldTab.values();
	}
	
	public void startDocument()
		throws SAXException
	{
		// log.debug("startDocument()");
		m_oFieldTab = new HashMap();
		m_oStateTab = new HashMap();
		m_oStateIdList = new Vector();
		m_nStep = STATE_START;
	}
	
	public void startElement(String uri, String localName, String qName, Attributes attributes)
		throws SAXException
	{
		// log.debug("startElement(" + uri + ", " + localName + ", " + qName + ")");
		switch(m_nStep)
		{
			case STATE_START:
			{
				if (!TAG_ROOT.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_ROOT);
				
				m_nStep = STATE_START_HEAD;
				break;
			}
			case STATE_START_HEAD:
			{
				if (!TAG_HEAD.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_HEAD);
				
				m_nStep = STATE_START_NAME;
				break;
			}
			case STATE_START_NAME:
			{
				if (!TAG_NAME.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_NAME);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_NAME;
				break;
			}
			case STATE_START_VERSION:
			{
				if (!TAG_VERSION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_VERSION);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_VERSION;
				break;
			}
			case STATE_START_DESCRIPTION:
			{
				if (!TAG_DESCRIPTION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_DESCRIPTION);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_DESCRIPTION;
				break;
			}
			case STATE_START_PERSISTENCEHANDLER:
			{
				if (!TAG_PERSISTENCEHANDLER.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_PERSISTENCEHANDLER);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_PERSISTENCEHANDLER;
				break;
			}
			case STATE_START_ACTIVITYHANDLER:
			{
				if (!TAG_ACTIVITYHANDLER.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_ACTIVITYHANDLER);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_ACTIVITYHANDLER;
				break;
			}
			case STATE_START_FIELDMAPPINGS:
			{
				if (!TAG_FIELDMAPPINGS.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_FIELDMAPPINGS);
				
				m_nStep = STATE_START_FIELD;
				break;
			}
			case STATE_START_FIELD:
			{
				if (!TAG_FIELD.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_FIELD);
				
				String oFieldId = attributes.getValue(ATTRIBUTE_ID);
				if (null == oFieldId)
					throw new SAXParseException(1005, m_nStep, qName);
					
				m_oCurrFieldMapping = new FieldMapping(oFieldId);
				String oAttr = attributes.getValue(ATTRIBUTE_TYPE);
				if (null != oAttr)
					m_oCurrFieldMapping.setFieldTypeName(oAttr);
				oAttr = attributes.getValue(ATTRIBUTE_PROTOTYPE);
				if (null != oAttr)
					m_oCurrFieldMapping.setPrototypeName(oAttr);
				oAttr = attributes.getValue(ATTRIBUTE_DEFAULT);
				if (null != oAttr)
					m_oCurrFieldMapping.setDefaultValue(oAttr);
				oAttr = attributes.getValue(ATTRIBUTE_FORMAT);
				if (null != oAttr)
					m_oCurrFieldMapping.setConvertFormat(oAttr);
				oAttr = attributes.getValue(ATTRIBUTE_CONVERTOR);
				if (null != oAttr)
					m_oCurrFieldMapping.setConvertor(oAttr);
					
				m_oCurrFieldMapping.setProcessKey("true".equalsIgnoreCase(attributes.getValue(ATTRIBUTE_ISKEY)));

				m_oFieldTab.put(m_oCurrFieldMapping.getFieldId(), m_oCurrFieldMapping);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_FIELD;
				break;
			}
			case STATE_START_STATES:
			{
				if (!TAG_STATES.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATES);
				
				m_nStep = STATE_START_STATE;
				break;
			}
			case STATE_START_STATE:
			{
				if (!TAG_STATE.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATE);
				
				String oStateId = attributes.getValue(ATTRIBUTE_ID);
				if (null == oStateId)
					throw new SAXParseException(1005, m_nStep, qName);
					
				m_oCurrState = new State(oStateId);
				m_oStateTab.put(m_oCurrState.getStateId(), m_oCurrState);
				m_oStateIdList.addElement(oStateId);
				
				m_nStep = STATE_START_STATETYPE;
				break;
			}
			case STATE_START_STATETYPE:
			{
				if (!TAG_STATETYPE.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATETYPE);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_STATETYPE;
				break;
			}
			case STATE_START_STATEDESCRIPTION:
			{
				if (!TAG_STATEDESCRIPTION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATEDESCRIPTION);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_STATEDESCRIPTION;
				break;
			}
			case STATE_START_STATEINHIBITS:
			{
				if (TAG_STATEINHIBITS.equals(qName))
					m_nStep = STATE_START_STATECONDITION;
				else if (TAG_STATEACTIVITIES.equals(qName))
					m_nStep = STATE_START_STATEACTIVITY;
				else 
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATEINHIBITS);
				
				break;
			}
			case STATE_START_STATECONDITION:
			{
				if (!TAG_STATECONDITION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATECONDITION);
				
				m_oCurrCond = new Condition(attributes.getValue(ATTRIBUTE_ID));
				m_nStep = STATE_GET_STATECONDITION;
				break;
			}
			case STATE_GET_STATECONDITION:
			{
				if (TAG_STATECONDITIONEXPRESSION.equals(qName))
					m_nStep = STATE_GET_STATECONDITIONEXPR;
				else if (TAG_STATECONDITIONCLASS.equals(qName))
					m_nStep = STATE_GET_STATECONDITIONCLASS;
				else
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATECONDITIONEXPRESSION + "/" + TAG_STATECONDITIONCLASS);
				
				m_oContentBuffer = new StringBuffer();
				break;
			}
			case STATE_START_STATECONDITIONDESCRIPTION:
			{
				if (!TAG_STATECONDITIONDESCRIPTION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATECONDITIONDESCRIPTION);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_STATECONDITIONDESCRIPTION;
				break;
			}
			case STATE_START_STATEACTIVITIES:
			{
				if (!TAG_STATEACTIVITIES.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATEACTIVITIES);
				
				m_nStep = STATE_START_STATEACTIVITY;
				break;
			}
			case STATE_START_STATEACTIVITY:
			{
				if (!TAG_STATEACTIVITY.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_STATEACTIVITY);
				
				m_oCurrActi = new Activity(attributes.getValue(ATTRIBUTE_ID), attributes.getValue(ATTRIBUTE_PARAMETER));
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_STATEACTIVITY;
				break;
			}
			case STATE_START_TRANSITIONS:
			{
				if (!TAG_TRANSITIONS.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITIONS);
				
				m_nStep = STATE_START_TRANSITION;
				break;
			}
			case STATE_START_TRANSITION:
			{
				if (!TAG_TRANSITION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITION);
				
				m_nStep = STATE_START_TRANSITIONFROMSTATE;
				break;
			}
			case STATE_START_TRANSITIONFROMSTATE:
			{
				if (!TAG_TRANSITIONFROMSTATE.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITIONFROMSTATE);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_TRANSITIONFROMSTATE;
				break;
			}
			case STATE_START_TRANSITIONTOSTATE:
			{
				if (!TAG_TRANSITIONTOSTATE.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITIONTOSTATE);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_TRANSITIONTOSTATE;
				break;
			}
			case STATE_START_TRANSITIONINHIBITS:
			{
				if (!TAG_TRANSITIONINHIBITS.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITIONINHIBITS);
				
				m_nStep = STATE_START_TRANSITIONCONDITION;
				break;
			}
			case STATE_START_TRANSITIONCONDITION:
			{
				if (!TAG_TRANSITIONCONDITION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITIONCONDITION);
				
				m_oCurrCond = new Condition(attributes.getValue(ATTRIBUTE_ID));
				m_nStep = STATE_GET_TRANSITIONCONDITION;
				break;
			}
			case STATE_GET_TRANSITIONCONDITION:
			{
				if (TAG_TRANSITIONCONDITIONEXPRESSION.equals(qName))
					m_nStep = STATE_GET_TRANSITIONCONDITIONEXPR;
				else if (TAG_TRANSITIONCONDITIONCLASS.equals(qName))
					m_nStep = STATE_GET_TRANSITIONCONDITIONCLASS;
				else
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITIONCONDITIONEXPRESSION + "/" + TAG_TRANSITIONCONDITIONCLASS);
				
				m_oContentBuffer = new StringBuffer();
				break;
			}
			case STATE_START_TRANSITIONCONDITIONDESCRIPTION:
			{
				if (!TAG_TRANSITIONCONDITIONDESCRIPTION.equals(qName))
					throw new SAXParseException(1004, m_nStep, qName, TAG_TRANSITIONCONDITIONDESCRIPTION);
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_TRANSITIONCONDITIONDESCRIPTION;
				break;
			}

			default:
				throw new SAXParseException(1002, m_nStep, qName, m_oContentBuffer);
		}
	}
	
	public void characters(char[] ch, int start, int length)
		throws SAXException
	{
		switch(m_nStep)
		{
			case STATE_GET_NAME:
			case STATE_GET_VERSION:
			case STATE_GET_DESCRIPTION:
			case STATE_GET_PERSISTENCEHANDLER:
			case STATE_GET_ACTIVITYHANDLER:
			case STATE_GET_FIELD:
			case STATE_GET_STATETYPE:
			case STATE_GET_STATEDESCRIPTION:
			case STATE_GET_STATECONDITIONEXPR:
			case STATE_GET_STATECONDITIONCLASS:
			case STATE_GET_STATECONDITIONDESCRIPTION:
			case STATE_GET_STATEACTIVITY:
			case STATE_GET_TRANSITIONFROMSTATE:
			case STATE_GET_TRANSITIONTOSTATE:
			case STATE_GET_TRANSITIONCONDITIONEXPR:
			case STATE_GET_TRANSITIONCONDITIONCLASS:
			case STATE_GET_TRANSITIONCONDITIONDESCRIPTION:
			{
				m_oContentBuffer.append(ch, start, length);
				break;
			}
		}
	}
	
	public void endElement(String uri, String localName, String qName)
		throws SAXException
	{
		log.debug(qName + "[" + uri + "]");
		
		switch(m_nStep)
		{
			case STATE_GET_NAME:
			{
				String oName = m_oContentBuffer.toString().trim();
				if (!oName.equals(m_oProcessName))
				{
					throw new SAXParseException(1101, new String[] {String.valueOf(m_nStep), oName, m_oProcessName});
				}
				
				m_nStep = STATE_START_VERSION;
				break;
			}
			case STATE_GET_VERSION:
			{
				String oVersion = m_oContentBuffer.toString().trim();
				if (!oVersion.equals(m_oProcessVersion))
				{
					throw new SAXParseException(1102, new String[] {String.valueOf(m_nStep), oVersion, m_oProcessVersion});
				}
				
				m_nStep = STATE_START_DESCRIPTION;
				break;
			}
			case STATE_GET_DESCRIPTION:
			{
				m_oProcessDescription = m_oContentBuffer.toString().trim();
				m_nStep = STATE_START_PERSISTENCEHANDLER;
				break;
			}
			case STATE_GET_PERSISTENCEHANDLER:
			{
				String val = m_oContentBuffer.toString().trim();
				if (0 < val.length())
					m_oPhClassName = val;
				m_nStep = STATE_START_ACTIVITYHANDLER;
				break;
			}
			case STATE_GET_ACTIVITYHANDLER:
			{
				String val = m_oContentBuffer.toString().trim();
				if (0 < val.length())
					m_oAhClassName = val;
				m_nStep = STATE_END_HEAD;
				break;
			}
			case STATE_END_HEAD:
			{
				m_nStep = STATE_START_FIELDMAPPINGS;
				break;
			}
			case STATE_START_FIELD:
			{
				m_nStep = STATE_START_STATES;
				break;
			}
			case STATE_GET_FIELD:
			{
				m_oCurrFieldMapping.setNativeName(m_oContentBuffer.toString().trim());
				m_nStep = STATE_START_FIELD;
				break;
			}
			case STATE_GET_STATETYPE:
			{
				String oTypeStr = m_oContentBuffer.toString().trim();
				if (STATE_START_VALUE.equals(oTypeStr))
					m_oCurrState.setStateType(State.START);
				else if (STATE_END_VALUE.equals(oTypeStr))
					m_oCurrState.setStateType(State.END);
				else if (STATE_ACTIVITY_VALUE.equals(oTypeStr))
					m_oCurrState.setStateType(State.ACTIVITY);
				else if (STATE_FORK_VALUE.equals(oTypeStr))
					m_oCurrState.setStateType(State.FORK);
				else if (STATE_JOIN_VALUE.equals(oTypeStr))
					m_oCurrState.setStateType(State.JOIN);
				else
					throw new SAXParseException(1103, new String[] {String.valueOf(m_nStep), m_oCurrState.getStateId(), oTypeStr});

				m_nStep = STATE_START_STATEDESCRIPTION;
				break;
			}
			case STATE_GET_STATEDESCRIPTION:
			{
				m_oCurrState.setStateDescription(m_oContentBuffer.toString().trim());
				switch(m_oCurrState.getStateType())
				{
					case State.END:
						m_nStep = STATE_END_STATE; break;
					default:
						m_nStep = STATE_START_STATEINHIBITS; break;
				}
				break;
			}
			case STATE_START_STATEINHIBITS:	// no Inhibits or Activities
			{
				m_nStep = STATE_START_STATE;
				break;
			}
			case STATE_GET_STATECONDITIONEXPR:
			{
				m_oCurrCond.setType(Condition.EXPRESSION);
				m_oCurrCond.setExpression(m_oContentBuffer.toString().trim());
				m_nStep = STATE_START_STATECONDITIONDESCRIPTION;
				break;
			}
			case STATE_GET_STATECONDITIONCLASS:
			{
				m_oCurrCond.setType(Condition.CLASS);
				m_oCurrCond.setExpression(m_oContentBuffer.toString().trim());
				m_nStep = STATE_START_STATECONDITIONDESCRIPTION;
				break;
			}
			case STATE_GET_STATECONDITIONDESCRIPTION:
			{
				m_oCurrCond.setDescription(m_oContentBuffer.toString().trim());
				try
				{
					m_oCurrState.addCondition(m_oCurrCond);
				}
				catch(ProcessCompileException e)
				{
					log.warn("ROOT CAUSE: ", e);
					throw new SAXParseException(1122, new String[] {m_oCurrState.toString(), m_oCurrCond.toString()});
				}
				m_nStep = STATE_END_STATECONDITION;
				break;
			}
			case STATE_END_STATECONDITION:
			{
				m_nStep = STATE_START_STATECONDITION;
				break;
			}
			case STATE_START_STATECONDITION:
			{
				switch(m_oCurrState.getStateType())
				{
					case State.ACTIVITY:
						m_nStep = STATE_START_STATEACTIVITIES; break;
					default:
						m_nStep = STATE_END_STATE; break;
				}
				break;
			}
			case STATE_START_STATEACTIVITIES: // no Activities
			{
				m_nStep = STATE_START_STATE;
				break;
			}
			case STATE_GET_STATEACTIVITY:
			{
				m_oCurrActi.setContent(m_oContentBuffer.toString().trim());
				m_oCurrState.addActivity(m_oCurrActi);
				m_nStep = STATE_START_STATEACTIVITY;
				break;
			}
			case STATE_START_STATEACTIVITY:
			{
				m_nStep = STATE_END_STATE;
				break;
			}
			case STATE_END_STATE:
			{
				m_nStep = STATE_START_STATE;
				break;
			}
			case STATE_START_STATE:
			{
				m_nStep = STATE_START_TRANSITIONS;
				break;
			}
			case STATE_GET_TRANSITIONFROMSTATE:
			{
				String oFromStateId = m_oContentBuffer.toString().trim();
				m_oCurrState = getState(oFromStateId);
				if (null == m_oCurrState)
					throw new SAXParseException(1135, oFromStateId);
					
				m_oCurrTransition = new Transition(m_oCurrState);
				try
				{
					m_oCurrState.addOutTransition(m_oCurrTransition);
				}
				catch(ProcessCompileException e)
				{
					log.warn("ROOT CAUSE: ", e);
					throw new SAXParseException(1123, new String[] {m_oCurrState.toString(), m_oCurrCond.toString()});
				}
				
				m_nStep = STATE_START_TRANSITIONTOSTATE;
				break;
			}
			case STATE_GET_TRANSITIONTOSTATE:
			{
				State oFromState = m_oCurrTransition.getFromState();
				String oToStateId = m_oContentBuffer.toString().trim();
				State oToState = getState(oToStateId);
				if (null == oToState)
					throw new SAXParseException(1136, oToStateId);
				
				m_oCurrTransition.setToState(oToState);
				
				m_nStep = STATE_START_TRANSITIONINHIBITS;
				switch(oFromState.getStateType())
				{
					case State.END:
						throw new SAXParseException(1104, new String[] {String.valueOf(m_nStep), oFromState.getStateId()});
					case State.START:
					case State.FORK:
					case State.JOIN:
						m_nStep = STATE_END_TRANSITION; break;
					case State.ACTIVITY:
						break;
					default:
						throw new SAXParseException(1106, m_nStep);
				}
				switch(oToState.getStateType())
				{
					case State.START:
						throw new SAXParseException(1105, new String[] {String.valueOf(m_nStep), oToState.getStateId()});
					case State.END:
						m_nStep = STATE_END_TRANSITION; break;
					case State.FORK:
					case State.JOIN:
					case State.ACTIVITY:
						break;
					default:
						throw new SAXParseException(1106, m_nStep);
				}
				break;
			}
			case STATE_GET_TRANSITIONCONDITIONEXPR:
			{
				m_oCurrCond.setType(Condition.EXPRESSION);
				m_oCurrCond.setExpression(m_oContentBuffer.toString().trim());
				m_nStep = STATE_START_TRANSITIONCONDITIONDESCRIPTION;
				break;
			}
			case STATE_GET_TRANSITIONCONDITIONCLASS:
			{
				m_oCurrCond.setType(Condition.CLASS);
				m_oCurrCond.setExpression(m_oContentBuffer.toString().trim());
				m_nStep = STATE_START_TRANSITIONCONDITIONDESCRIPTION;
				break;
			}
			case STATE_GET_TRANSITIONCONDITIONDESCRIPTION:
			{
				m_oCurrCond.setDescription(m_oContentBuffer.toString().trim());
				try
				{
					m_oCurrTransition.addCondition(m_oCurrCond);
				}
				catch(ProcessCompileException e)
				{
					log.warn("ROOT CAUSE: ", e);
					throw new SAXParseException(1122, new String[] {m_oCurrState.toString(), m_oCurrCond.toString()});
				}
				m_nStep = STATE_END_TRANSITIONCONDITION;
				break;
			}
			case STATE_END_TRANSITIONCONDITION:
			{
				m_nStep = STATE_START_TRANSITIONCONDITION;
				break;
			}
			case STATE_START_TRANSITIONCONDITION:
			{
				m_nStep = STATE_END_TRANSITION;
				break;
			}
			case STATE_START_TRANSITIONINHIBITS:
			case STATE_END_TRANSITION:
			{
				m_nStep = STATE_START_TRANSITION;
				break;
			}
			case STATE_START_TRANSITION:
			{
				m_nStep = STATE_END_ROOT;
				break;
			}
			case STATE_END_ROOT:
			{
				m_nStep = STATE_END;
				break;
			}
			default:
			{
				throw new SAXParseException(1003, m_nStep, qName, m_oContentBuffer);
			}
		}
	}
	
	public void endDocument()
		throws SAXException
	{
		// log.debug("endDocument()");
		
		switch(m_nStep)
		{
			case STATE_END:
			{
				log.info(MessageResource.getDefaultInstance().getMessage("laya.info.ProcessConfigLoadOk"));
				break;
			}
			default:
			{
				throw new SAXParseException(m_nStep);
			}
		}
	}
}
